<?php
$isReloadable = false;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');
require_once('../../_inc/funct.tabDati.php');

if (reqVar('azione') == 'salva' || reqVar('azione') == 'chgSez' ) {
	$msgPag = salvaSez();
} 

$sqlSel = "SELECT a.*, r.data_rif, r.descr  FROM rgl_rating r "
		." INNER JOIN rgl_azienda a ON a.id_azienda = r.id_azienda " 
		." WHERE r.id_rating = ".reqVarAS('id');
$objDB->query($sqlSel,'selRec');
if (!$acAnag = $objDB->fetch('selRec')) {
	backOnErr("rqueIns az - var. id '".reqVar('id')."' mancante o errato");
}

//lettura risposte memorizzate
$aQueRisp = array();
$sSql = "SELECT rq.str_que, rq.id_mod_rque  FROM rgl_ratque rq " 
		." WHERE rq.id_rating = ".reqVar('id');
$objDB->query($sSql,'selRec');
$acRQ = $objDB->fetch('selRec');
if ($acRQ && !empty($acRQ['str_que'])) {
	$aQueRisp = unsgzu($acRQ['str_que']);
}


$sCmdL = '';
$sCmdL .= "<a class='cmd1' href=\"rquePdf.php?id=".reqvar('id')."&xsn=".XSN."\" target='_blank'>PDF</a>";

$sCmdR = '';
$sCmdR .= "<a class='cmd1' href=\"rqueIns.php?id=".reqvar('id')."&xsn=".XSN."\" >modifica</a>";
$sCmdR .= cmdTorna('torna');

testaHtml('', $msgPag, 'ratg');
?>
<script type="text/javascript">
<!--
function submAz(az) {
	document.frm1.azione.value = az;
	document.frm1.submit();
}
//-->
</script>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left">
			<? echo $sCmdL; ?>
      </td>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<?
echo getHtmlForm();
echo getHtmlHidden('azione','...');
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('id');
?>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
	<?=getHtmlRigaRif(25);?>
   <tr>
      <td class="tit1" colspan='25' align='center'>Questionario</td>
   </tr>
   </tr>
	<tr>
		<td class='lab1' colspan='4' valign='top'>Rating</td>
		<td class='txt1' colspan='1'>
			<?=reqVar('id')?>
		</td>
      <td class="txt1" colspan='3' align='center'>
      	<?=YMD2DMY($acAnag['data_rif'])?>
      </td>
      <td class="txt1" colspan='17' align='left'>
      	<?=$acAnag['descr']?>
      </td>
	</tr>
	<tr>
		<td class='lab1' colspan='4' valign='top'>Ragione sociale</td>
		<td class='txt1' colspan='14'>
			<?=$acAnag['rag_soc']?>
		</td>
		<td class='lab1' colspan='4' valign='top'>Codice fiscale</td>
		<td class='txt1' colspan='3'>
			<?=$acAnag['cod_fisc']?>
		</td>
	</tr>
	<tr>
		<td class='lab1' colspan='4' valign='top'>Sede</td>
		<td class='txt1' colspan='14'>
			<?=$acAnag['sede']?>
		</td>
		<td class='lab1' colspan='4' valign='top'>Codice CCIAA</td>
		<td class='txt1' colspan='3'>
			<?=$acAnag['cod_cciaa']?>
		</td>
	<tr>
		<td class='lab1' colspan='4' valign='top'>Cod. settore</td>
		<td class='txt1' colspan='21'>
			*** - Settore generico&nbsp;
		</td>
	</tr>
   <tr>
      <td class="lab1" colspan='4' align='left'>
      	Rating questionario
      </td>
      <td class="txt1" colspan='3' align='center'>
<? 
	$tqp = 0;
	$tqpp = 0;
	foreach($aQueRisp as $codSez => $aSez) {
		$tp = 0;
		$tpp = 0;
		foreach($aSez['a'] as $kR => $aR) {
			$tpp += $aR['v']*$aR['p'];
			$tp += $aR['p'];
		}
		$puntSez = ($tp!=0)?($tpp/$tp):0;
		$tqp += $aSez['p'];
		$tqpp += $aSez['p']*$puntSez;
	} 	
	$puntQue = ($tqp!=0)?($tqpp/$tqp):0;
	$sqlSel = "SELECT * FROM rgl_puntque " 
		." WHERE id_mod_rque = ".$acRQ['id_mod_rque']
		." ORDER BY punteggio";
	$objDB->query($sqlSel,'selRecPuQ');
	$sSigla = '???';
	$sColore = 'rosso';
	$sDescr = '';
	while ($acP = $objDB->fetch('selRecPuQ')) {
   	if ($puntQue<=$acP['punteggio']) {
      	$sSigla = $acP['sigla'];
      	$sColore = $acP['colore'];
			$sDescr = $acP['descr_estesa'];
      	break;
   	} 
      $sSigla = $acP['sigla'];
		$sColore = $acP['colore'];
		$sDescr = $acP['descr_estesa'];
	}	
	$sStyle = '';
	if ($sColore=='verde') {
		$sStyle = "color:".RGL_COLORE_VERDE.";font-size:18pt;";
	} elseif ($sColore=='giallo') {
		$sStyle = "color:".RGL_COLORE_GIALLO.";font-size:18pt;";
	} else {
		$sStyle = "color:".RGL_COLORE_ROSSO.";font-size:18pt;";
	}
   echo "<b><span style='$sStyle'>$sSigla</span></b>";
?>
      </td>
      <td class="txt1" colspan='18' align='left'>
      	<?=$sDescr?>
      </td>
   </tr>
   <tr>
      <td class="tit1" colspan='25' align='center'>Questionario</td>
   </tr>
<?
$nS=0;
foreach($aQueRisp as $codSez => $aSez) {
	$nS++;
?>
   <tr>
      <td class="lab1" colspan='17' align='left'>
      	<b><? echo $nS.'. '.$aSez['d'];?></b>
      </td>
      <td class="lab1" colspan='6' align='center'>
<? 
	$tp = 0;
	$tpp = 0;
	foreach($aSez['a'] as $kR => $aR) {
		$tpp += $aR['v']*$aR['p'];
		$tp += $aR['p'];
	}
	$puntSez = ($tp!=0)?($tpp/$tp):0;
      	
	$sqlSel = "SELECT * FROM rgl_puntque " 
		." WHERE id_mod_rque = ".$acRQ['id_mod_rque']
		." ORDER BY punteggio";
	$objDB->query($sqlSel,'selRecPuQ');
	$sSigla = '???';
	$sColore = 'rosso';
	while ($acP = $objDB->fetch('selRecPuQ')) {
   	if ($puntSez<=$acP['punteggio']) {
      	$sSigla = $acP['sigla'];
      	$sColore = $acP['colore'];
      	break;
   	} 
      $sSigla = $acP['sigla'];
		$sColore = $acP['colore'];
	}	
	$sStyle = '';
	if ($sColore=='verde') {
		$sStyle = "color:".RGL_COLORE_VERDE.";";
	} elseif ($sColore=='giallo') {
		$sStyle = "color:".RGL_COLORE_GIALLO.";";
	} else {
		$sStyle = "color:".RGL_COLORE_ROSSO.";";
	}
   echo "<b><span style='$sStyle'>$sSigla</span></b>";
?>
      </td>
      <td class="lab1" colspan='1' align='right'>
      	<b><? echo $aSez['p'];?></b>
      </td>
      <td class="lab1" colspan='1' align='right'>
      	<b><?
      	echo round($puntSez);?></b>
      </td>
   </tr>
<?
	$nD=0;
	foreach($aSez['a'] as $codDom => $aDom) {
		$nD++;
?>
   <tr>
      <td class="txt1" colspan='2' align='right' valign='top'>     	
      	<?=$nD?>.&nbsp;
      </td>
      <td class="txt1" colspan='15' align='left' valign='top'>
      	<? echo $aDom['d'];?>
      </td>
      <td class="txt1" colspan='6' align='left'>
      	<? echo $aDom['r'];?>
      </td>
      <td class="txt1" colspan='1' align='right'>
      	<? echo $aDom['p'];?>
      </td>
      <td class="txt1" colspan='1' align='right'>
      	<? echo $aDom['v'];?>
      </td>
   </tr>
<?
	}
}
?>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left">
			<? echo $sCmdL; ?>
      </td>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<?
echo "</form>\n";

codaHtml();
exit();
